{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "logical and\n",
      "epoch0 sample0 [1,2,0,0,0,0,0]\n",
      "epoch0 sample1 [1,2,0,1,-1,0,-1]\n",
      "epoch0 sample2 [0,2,-1,1,0,-1,-1]\n",
      "epoch0 sample3 [0,1,-2,0,1,1,1]\n",
      "epoch1 sample0 [1,2,-1,0,0,0,0]\n",
      "epoch1 sample1 [1,2,-1,0,0,0,0]\n",
      "epoch1 sample2 [1,2,-1,1,0,-1,-1]\n",
      "epoch1 sample3 [1,1,-2,0,1,1,1]\n",
      "epoch2 sample0 [2,2,-1,0,0,0,0]\n",
      "epoch2 sample1 [2,2,-1,1,-1,0,-1]\n",
      "epoch2 sample2 [1,2,-2,0,0,0,0]\n",
      "epoch2 sample3 [1,2,-2,1,0,0,0]\n",
      "epoch3 sample0 [1,2,-2,0,0,0,0]\n",
      "epoch3 sample1 [1,2,-2,0,0,0,0]\n",
      "epoch3 sample2 [1,2,-2,0,0,0,0]\n",
      "epoch3 sample3 [1,2,-2,1,0,0,0]\n",
      "epoch4 sample0 [1,2,-2,0,0,0,0]\n",
      "epoch4 sample1 [1,2,-2,0,0,0,0]\n",
      "epoch4 sample2 [1,2,-2,0,0,0,0]\n",
      "epoch4 sample3 [1,2,-2,1,0,0,0]\n",
      "epoch5 sample0 [1,2,-2,0,0,0,0]\n",
      "epoch5 sample1 [1,2,-2,0,0,0,0]\n",
      "epoch5 sample2 [1,2,-2,0,0,0,0]\n",
      "epoch5 sample3 [1,2,-2,1,0,0,0]\n",
      "epoch6 sample0 [1,2,-2,0,0,0,0]\n",
      "epoch6 sample1 [1,2,-2,0,0,0,0]\n",
      "epoch6 sample2 [1,2,-2,0,0,0,0]\n",
      "epoch6 sample3 [1,2,-2,1,0,0,0]\n",
      "epoch7 sample0 [1,2,-2,0,0,0,0]\n",
      "epoch7 sample1 [1,2,-2,0,0,0,0]\n",
      "epoch7 sample2 [1,2,-2,0,0,0,0]\n",
      "epoch7 sample3 [1,2,-2,1,0,0,0]\n",
      "epoch8 sample0 [1,2,-2,0,0,0,0]\n",
      "epoch8 sample1 [1,2,-2,0,0,0,0]\n",
      "epoch8 sample2 [1,2,-2,0,0,0,0]\n",
      "epoch8 sample3 [1,2,-2,1,0,0,0]\n",
      "epoch9 sample0 [1,2,-2,0,0,0,0]\n",
      "epoch9 sample1 [1,2,-2,0,0,0,0]\n",
      "epoch9 sample2 [1,2,-2,0,0,0,0]\n",
      "epoch9 sample3 [1,2,-2,1,0,0,0]\n",
      "logical or\n",
      "epoch0 sample0 [1,2,0,0,0,0,0]\n",
      "epoch0 sample1 [1,2,0,1,0,0,0]\n",
      "epoch0 sample2 [1,2,0,1,0,0,0]\n",
      "epoch0 sample3 [1,2,0,1,0,0,0]\n",
      "epoch1 sample0 [1,2,0,0,0,0,0]\n",
      "epoch1 sample1 [1,2,0,1,0,0,0]\n",
      "epoch1 sample2 [1,2,0,1,0,0,0]\n",
      "epoch1 sample3 [1,2,0,1,0,0,0]\n",
      "epoch2 sample0 [1,2,0,0,0,0,0]\n",
      "epoch2 sample1 [1,2,0,1,0,0,0]\n",
      "epoch2 sample2 [1,2,0,1,0,0,0]\n",
      "epoch2 sample3 [1,2,0,1,0,0,0]\n",
      "epoch3 sample0 [1,2,0,0,0,0,0]\n",
      "epoch3 sample1 [1,2,0,1,0,0,0]\n",
      "epoch3 sample2 [1,2,0,1,0,0,0]\n",
      "epoch3 sample3 [1,2,0,1,0,0,0]\n",
      "epoch4 sample0 [1,2,0,0,0,0,0]\n",
      "epoch4 sample1 [1,2,0,1,0,0,0]\n",
      "epoch4 sample2 [1,2,0,1,0,0,0]\n",
      "epoch4 sample3 [1,2,0,1,0,0,0]\n",
      "epoch5 sample0 [1,2,0,0,0,0,0]\n",
      "epoch5 sample1 [1,2,0,1,0,0,0]\n",
      "epoch5 sample2 [1,2,0,1,0,0,0]\n",
      "epoch5 sample3 [1,2,0,1,0,0,0]\n",
      "epoch6 sample0 [1,2,0,0,0,0,0]\n",
      "epoch6 sample1 [1,2,0,1,0,0,0]\n",
      "epoch6 sample2 [1,2,0,1,0,0,0]\n",
      "epoch6 sample3 [1,2,0,1,0,0,0]\n",
      "epoch7 sample0 [1,2,0,0,0,0,0]\n",
      "epoch7 sample1 [1,2,0,1,0,0,0]\n",
      "epoch7 sample2 [1,2,0,1,0,0,0]\n",
      "epoch7 sample3 [1,2,0,1,0,0,0]\n",
      "epoch8 sample0 [1,2,0,0,0,0,0]\n",
      "epoch8 sample1 [1,2,0,1,0,0,0]\n",
      "epoch8 sample2 [1,2,0,1,0,0,0]\n",
      "epoch8 sample3 [1,2,0,1,0,0,0]\n",
      "epoch9 sample0 [1,2,0,0,0,0,0]\n",
      "epoch9 sample1 [1,2,0,1,0,0,0]\n",
      "epoch9 sample2 [1,2,0,1,0,0,0]\n",
      "epoch9 sample3 [1,2,0,1,0,0,0]\n",
      "logical xor\n",
      "epoch0 sample0 [1,2,0,0,0,0,0]\n",
      "epoch0 sample1 [1,2,0,1,0,0,0]\n",
      "epoch0 sample2 [1,2,0,1,0,0,0]\n",
      "epoch0 sample3 [1,2,0,1,-1,-1,-1]\n",
      "epoch1 sample0 [0,1,-1,0,0,0,0]\n",
      "epoch1 sample1 [0,1,-1,0,1,0,1]\n",
      "epoch1 sample2 [1,1,0,1,0,0,0]\n",
      "epoch1 sample3 [1,1,0,1,-1,-1,-1]\n",
      "epoch2 sample0 [0,0,-1,0,0,0,0]\n",
      "epoch2 sample1 [0,0,-1,0,1,0,1]\n",
      "epoch2 sample2 [1,0,0,0,0,1,1]\n",
      "epoch2 sample3 [1,1,1,1,-1,-1,-1]\n",
      "epoch3 sample0 [0,0,0,0,0,0,0]\n",
      "epoch3 sample1 [0,0,0,0,1,0,1]\n",
      "epoch3 sample2 [1,0,1,1,0,0,0]\n",
      "epoch3 sample3 [1,0,1,1,-1,-1,-1]\n",
      "epoch4 sample0 [0,-1,0,0,0,0,0]\n",
      "epoch4 sample1 [0,-1,0,0,1,0,1]\n",
      "epoch4 sample2 [1,-1,1,0,0,1,1]\n",
      "epoch4 sample3 [1,0,2,1,-1,-1,-1]\n",
      "epoch5 sample0 [0,-1,1,1,0,0,-1]\n",
      "epoch5 sample1 [0,-1,0,0,1,0,1]\n",
      "epoch5 sample2 [1,-1,1,0,0,1,1]\n",
      "epoch5 sample3 [1,0,2,1,-1,-1,-1]\n",
      "epoch6 sample0 [0,-1,1,1,0,0,-1]\n",
      "epoch6 sample1 [0,-1,0,0,1,0,1]\n",
      "epoch6 sample2 [1,-1,1,0,0,1,1]\n",
      "epoch6 sample3 [1,0,2,1,-1,-1,-1]\n",
      "epoch7 sample0 [0,-1,1,1,0,0,-1]\n",
      "epoch7 sample1 [0,-1,0,0,1,0,1]\n",
      "epoch7 sample2 [1,-1,1,0,0,1,1]\n",
      "epoch7 sample3 [1,0,2,1,-1,-1,-1]\n",
      "epoch8 sample0 [0,-1,1,1,0,0,-1]\n",
      "epoch8 sample1 [0,-1,0,0,1,0,1]\n",
      "epoch8 sample2 [1,-1,1,0,0,1,1]\n",
      "epoch8 sample3 [1,0,2,1,-1,-1,-1]\n",
      "epoch9 sample0 [0,-1,1,1,0,0,-1]\n",
      "epoch9 sample1 [0,-1,0,0,1,0,1]\n",
      "epoch9 sample2 [1,-1,1,0,0,1,1]\n",
      "epoch9 sample3 [1,0,2,1,-1,-1,-1]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "#逻辑与\n",
    "samples_and_ = [\n",
    "    [0,0,0],\n",
    "    [1,0,0],\n",
    "    [0,1,0],\n",
    "    [1,1,1]\n",
    "]\n",
    "#逻辑或\n",
    "samples_or_ = [\n",
    "    [0,0,0],\n",
    "    [1,0,1],\n",
    "    [0,1,1],\n",
    "    [1,1,1]\n",
    "]\n",
    "#逻辑异或\n",
    "samples_xor_ = [\n",
    "    [0,0,0],\n",
    "    [1,0,1],\n",
    "    [0,1,1],\n",
    "    [1,1,0]\n",
    "]\n",
    "\n",
    "def perceptron(samples):\n",
    "    w = np.array([1,2])\n",
    "    b = 0\n",
    "    a = 1\n",
    "    for i in range(10):\n",
    "        for j in range(4):\n",
    "            x = np.array(samples[j][:2])\n",
    "            \n",
    "            y = 1 if np.dot(w,x)+b>0 else 0\n",
    "            d = np.array(samples[j][2])\n",
    "            \n",
    "            delta_b = a*(d-y)\n",
    "            delta_w = a*(d-y)*x\n",
    "            \n",
    "            print('epoch{} sample{} [{},{},{},{},{},{},{}]'.format(i,j,w[0],w[1],b,y,delta_w[0],delta_w[1],delta_b))\n",
    "            w = w+delta_w\n",
    "            b = b+delta_b\n",
    "            \n",
    "if __name__ =='__main__':\n",
    "    print('logical and')\n",
    "    perceptron(samples_and_)\n",
    "    print('logical or')\n",
    "    perceptron(samples_or_)\n",
    "    print('logical xor')\n",
    "    perceptron(samples_xor_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "解释为什么这⾥的感知器代码⽆法完成异或功能\n",
    "感知器的作用实际上是在二维图像上画出一条直线将两个不同的类分出来\n",
    "而根据异或的形式[[0,0,0],\n",
    "                 [1,0,1],\n",
    "                 [0,1,1],\n",
    "                 [1,1,0]]\n",
    "没有这样的一条直线能够将0，1两类完全分类出来，所以感知器无法完成异或功能，同理，同或也无法完成。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
